package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemActivityAdapter;
import com.wolfalpha.utron.bean.ItemUActivity;
import com.wolfalpha.utron.service.ActivityService;
import com.wolfalpha.utron.service.impl.ActivityServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.list.WaterDropListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodon on 15/9/5.
 */
public class UtronActivityActivity extends BaseNormalActivity implements WaterDropListView.IWaterDropListViewListener {

    private WaterDropListView listView;

    private ItemActivityAdapter activityAdapter;

    private List<ItemUActivity> activityList;

    private ActivityService activityService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_utron_activity);

        activityService = new ActivityServiceImpl();

        initView();
        initListeners();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("优创会活动");

        activityList = new ArrayList<ItemUActivity>();
        listView = (WaterDropListView) findViewById(R.id.wlv_utron_activity);
        activityAdapter = new ItemActivityAdapter(this, activityList);
        listView.setAdapter(activityAdapter);
        listView.setWaterDropListViewListener(this);
        listView.setPullLoadEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    /**
     * 初始化监听器
     */
    private void initListeners() {

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > activityList.size()) return;
                Bundle bundle = new Bundle();
                bundle.putInt("actId", activityList.get(position - 1).getId());
                UIHelper.showActivity(UtronActivityActivity.this, UtronActivityDetailActivity.class, bundle);
            }
        });
    }

    private int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                final List<ItemUActivity> activityList1 = activityService.getList(page);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (activityList1 != null) {
                            activityList = activityList1;
                            activityAdapter.setData(activityList);
                            if (activityList1.size() < 10) {
                                listView.setPullLoadEnable(false);
                            } else {
                                listView.setPullLoadEnable(true);
                            }
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                        listView.stopRefresh();
                    }
                });
            }
        });
    }

    @Override
    public void onLoadMore() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                List<ItemUActivity> activities = new ArrayList<ItemUActivity>();
                activities = activityService.getList(++page);
                if (activities != null) {
                    activityList.addAll(activities);
                } else {
                    page--;
                }
                final List<ItemUActivity> finalActivities = activities;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (activityList != null) {
                            activityAdapter.setData(activityList);
                            if (finalActivities.size() < 10) {
                                listView.setPullLoadEnable(false);
                            } else
                                listView.setPullLoadEnable(true);
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                        listView.stopLoadMore();
                    }
                });

            }
        });
    }
}
